<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="showInfoList"
        ></el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="infos">
      <el-table-column label="主机" prop="host" align="left" width="150" />
      <el-table-column label="总内存" align="center">
        <template slot-scope="scope">{{ mem(scope.row, "total") }}</template>
      </el-table-column>
      <el-table-column label="剩余内存" align="center">
        <template slot-scope="scope">{{ mem(scope.row, "free") }}</template>
      </el-table-column>
      <el-table-column label="Cache" align="center">
        <template slot-scope="scope">{{ mem(scope.row, "cached") }}</template>
      </el-table-column>
      <el-table-column label="CPU使用率" align="center">
        <template slot-scope="scope">{{ cpu(scope.row) }}</template>
      </el-table-column>
      <el-table-column label="磁盘挂载路径" prop="path" align="center" />
      <el-table-column label="磁盘总大小" align="center">
        <template slot-scope="scope">{{
          fileSystemSpace(scope.row, "totalSpace")
        }}</template>
      </el-table-column>
      <el-table-column label="磁盘剩余" align="center">
        <template slot-scope="scope">{{
          fileSystemSpace(scope.row, "freeSpace")
        }}</template>
      </el-table-column>
      <!--
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="100"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="infoHandler(scope.row)"
            >详情</el-button
          >
        </template>
      </el-table-column>
      -->
    </el-table>
  </div>
</template>
<script>
import { getAllSystemInfo } from "@/api/console";
export default {
  name: "host",
  data() {
    return {
      loading: false,
      infos: [],
    };
  },
  created() {
    this.showInfoList();
  },
  methods: {
    showInfoList() {
      this.loading = true;
      getAllSystemInfo()
        .then((res) => {
          this.infos = res.data;
          this.loading = false;
        })
        .catch((msg) => {
          this.loading = false;
        });
    },
    mem(row, type) {
      const data = row.systemMemory;
      return (parseInt(data[type] * 10) * 1.0) / 10 + "M";
    },
    cpu(row) {
      return (parseInt(row.cpuUsage * 1000) * 1.0) / 10 + "%";
    },
    fileSystemSpace(row, type) {
      if (
        row.path &&
        row.allFileSystemSpace &&
        row.allFileSystemSpace[row.path]
      ) {
        const data = row.allFileSystemSpace[row.path];
        return (parseInt(data[type] * 10) * 1.0) / 10 + "G";
      }
      return 0 + "G";
    },
    infoHandler(row) {},
  },
};
</script>